package com.asion.gankapp.utils;

import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.RequiresApi;

import com.asion.gankapp.utils.downbutton.CircularProgressButton;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MyDownLoadTask extends AsyncTask<String,Integer,String> {

    private File file = new File(Environment.getExternalStorageDirectory() + File.separator + "Gank_Pictures" );

    private CircularProgressButton circularProgressButton;

    private int max;

    private int current = 0;

    public MyDownLoadTask(CircularProgressButton circularButton1) {
        this.circularProgressButton =circularButton1;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if(!file.exists()){
            file.mkdir();
        }
    }

    @Override
    protected void onPostExecute(String string) {
        super.onPostExecute(string);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        Integer value = values[0];
        if(value == max/2){
            circularProgressButton.setProgress(50);
        }else if(value == max){
            circularProgressButton.setProgress(100);
        }else if(value == -1){
            circularProgressButton.setProgress(-1);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected String doInBackground(String... params) {
        InputStream inputStream = null;
        FileOutputStream fos = null;
        String param = params[0];
        try {
            URL url = new URL(param);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setConnectTimeout(5000);
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setReadTimeout(5000);
            httpURLConnection.connect();
            if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK&&!isCancelled()) {
                inputStream = httpURLConnection.getInputStream();
                int contentLength = httpURLConnection.getContentLength();
                max = contentLength;
                fos = new FileOutputStream(file+File.separator + new File(param).getName());
                int len;
                byte[] bs = new byte[1024 * 8];
                while ((len = inputStream.read(bs)) != -1&&!isCancelled()) {
                    fos.write(bs, 0, len);
                    current+=len;
                    publishProgress(current);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (fos != null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
